package com.aube.mis.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.aube.api.v2.service.show.ShowVoV2Service;
import com.aube.api.v2.vo.show.req.ShowQueryReqVo;
import com.aube.api.v2.vo.show.resp.ShowVo;
import com.aube.common.req.vo.PaginationReqVo;
import com.aube.common.resp.vo.PaginationRespVo;
import com.aube.mis.controller.ShowController;
import com.aube.mis.dto.PaginationResultDTO;
import com.aube.mis.dto.ShowDTO;
import com.aube.mis.service.AbstractService;
import com.aube.mis.service.ShowService;
import com.aube.mis.vo.DataTableCriterias;
import com.aube.support.ResultCode;

@Service
public class ShowServiceImpl extends AbstractService implements ShowService {

	private static Logger log = LoggerFactory.getLogger(ShowController.class);

	@Resource
	ShowVoV2Service showRemoteService;

	@Override
	@SuppressWarnings("unchecked")
	public PaginationResultDTO<ShowDTO> getShowList(DataTableCriterias criterias) {
		PaginationReqVo<ShowQueryReqVo> reqVo = fillPaginationInfo(criterias);
		ResultCode<PaginationRespVo<ShowVo>> serverResp = showRemoteService.queryList(reqVo);
		List<ShowDTO> resultList = (List<ShowDTO>) CollectionUtils.collect(serverResp.getRetval().getResp(), new Transformer() {

			@Override
			public ShowDTO transform(Object object) {
				return convertShowDTO((ShowVo) object);
			}
		});
		PaginationResultDTO<ShowDTO> result = new PaginationResultDTO<ShowDTO>();
		result.setTotal(serverResp.getRetval().getTotal());
		result.setResultList(resultList);
		return result;
	}

	@Override
	public void deleteShow(String showId) {
		showRemoteService.removeById(showId);
	}

	@Override
	public void editShow(ShowDTO showDTO) {
		ShowVo vo = new ShowVo();
		vo.setShowid(showDTO.getShowId());
		vo.setShowName(showDTO.getShowName());
		vo.setShowDesc(showDTO.getShowDesc());
		vo.setPicurl(showDTO.getPicUrl());
		vo.setShowColor(showDTO.getShowColor());
		vo.setTextColor(showDTO.getTextColor());
		showRemoteService.saveOrModify(vo);
	}

	@Override
	public ShowDTO getShowDetail(String showId) {
		ResultCode<ShowVo> result = showRemoteService.getDetailById(showId);
		return convertShowDTO(result.getRetval());
	}

	private ShowDTO convertShowDTO(ShowVo showVo) {
		ShowDTO dto = new ShowDTO();
		dto.setShowId(showVo.getShowid());
		dto.setShowName(showVo.getShowName());
		dto.setShowDesc(showVo.getShowDesc());
		dto.setPicUrl(showVo.getPicurl());
		dto.setShowColor(showVo.getShowColor());
		dto.setTextColor(showVo.getTextColor());
		return dto;
	}
}
